//
//  HistoryTripsViewController.m
//  yadea
//
//  Created by songjun on 16/5/8.
//  Copyright © 2016年 Sita. All rights reserved.
//

#import "HistoryTripsViewController.h"
#import "CommonUtility.h"
@interface HistoryTripsViewController ()

@end

@implementation HistoryTripsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tripMapView = [[MAMapView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT*.75+20)];
    self.tripMapView.delegate = self;
    [self.view addSubview:self.tripMapView];
    
    CLLocationCoordinate2D commonPolylineCoords[self.pointSArr.count];
    
    for (int i = 0; i < self.pointSArr.count; i++) {
        NSDictionary *upPointDic = [self.pointSArr objectAtIndex:i];
        
        CLLocation *startLocation = [[CLLocation alloc] initWithLatitude:[[upPointDic valueForKey:@"latitude"] doubleValue] longitude:[[upPointDic valueForKey:@"longitude"] doubleValue]];
        CLLocation *converterStartLoc = [startLocation locationMarsFromEarth];
        
        commonPolylineCoords[i].latitude = converterStartLoc.coordinate.latitude;
        commonPolylineCoords[i].longitude = converterStartLoc.coordinate.longitude;
        if (i == self.pointSArr.count-1) {
            self.endCoor =CLLocationCoordinate2DMake(converterStartLoc.coordinate.latitude,converterStartLoc.coordinate.longitude);
        }
        
        if (i==0) {
            self.startCoor =CLLocationCoordinate2DMake(converterStartLoc.coordinate.latitude,converterStartLoc.coordinate.longitude);
        }
    }
    
    
    route = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:self.pointSArr.count];
    
    
    [self.tripMapView addOverlay:route];
    
    MAPointAnnotation *startAnnotation = [[MAPointAnnotation alloc] init];
    startAnnotation.coordinate = self.startCoor;
    startAnnotation.title = @"起点";
    
    MAPointAnnotation *destinationAnnotation = [[MAPointAnnotation alloc] init];
    destinationAnnotation.coordinate = self.endCoor;
    destinationAnnotation.title      = @"终点";
    [self.tripMapView addAnnotation:startAnnotation];
    [self.tripMapView addAnnotation:destinationAnnotation];
    
    self.mileLabel.text = self.mileString;
    self.averageSpeedLabel.text = self.spedString;
    self.voltageLabel.text = self.votageString;
    
    _mileLabel.text = [NSString stringWithFormat:@"%.2f km",[_tripDb.distance doubleValue]*0.001];
    _averageSpeedLabel.text = [NSString stringWithFormat:@"%.2f km/h",[_tripDb.speed doubleValue]];
    _voltageLabel.text = [NSString stringWithFormat:@"%@%@",_tripDb.soc,@"%"];
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    [self.tripMapView setVisibleMapRect:route.boundingMapRect edgePadding:UIEdgeInsetsMake( 100, 100,  100, 100) animated:YES];
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *routeCellIdentifier = @"RouteSummaryId";
        
        MAAnnotationView *poiAnnotationView = (MAAnnotationView*)[self.tripMapView dequeueReusableAnnotationViewWithIdentifier:routeCellIdentifier];
        if (poiAnnotationView == nil)
        {
            poiAnnotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
                                                             reuseIdentifier:routeCellIdentifier];
            poiAnnotationView.centerOffset = CGPointMake(0, -25);
        }
        
        if ([annotation.title isEqualToString:@"起点"])
        {
            poiAnnotationView.image = [UIImage imageNamed:@"startPoint"];
        }else if([annotation.title isEqualToString:@"终点"])
        {
            poiAnnotationView.image = [UIImage imageNamed:@"endPoint"];
            poiAnnotationView.centerOffset = CGPointMake(0, -25);

        }
        
        return poiAnnotationView;
    }
    
    return nil;
}


- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[MAPolyline class]])
    {
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:(MAPolyline *)overlay];
        
        polylineRenderer.lineWidth   =  4.f;
        polylineRenderer.strokeColor = UIColorHexRGBA(linBoardPolylinColor, 1.0);
        
        return polylineRenderer;
    }
    return nil;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
